package com.example.newstudent.controller;

import com.example.newstudent.entity.Result;
import com.example.newstudent.entity.optional_course;
import com.example.newstudent.entity.student;
import com.example.newstudent.service.optional_courseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

import static com.example.newstudent.entity.optional_course.summarizeCourses;

@CrossOrigin
@RestController

@RequestMapping("/optional_course")
public class optional_courseController {
    @Autowired
    optional_courseService optional_courseService;
    @PostMapping("/display")
    public Result display(@RequestBody student student){
        List<optional_course> optional_coursess = new ArrayList<>();
        System.out.println("学号为"+student.getId()+"同学查看选修课表");
        List<optional_course> optional_courses = optional_courseService.display(student.getId());
        Map<String, Map<String, Object>> weeksCount = summarizeCourses(optional_courses);
        for (Map.Entry<String, Map<String, Object>> entry : weeksCount.entrySet()) {
            optional_course optional_course = new optional_course();
            optional_course.setName(entry.getKey());
            optional_course.setTeacher((String) entry.getValue().get("teacher"));
            optional_course.setWeeks((String) entry.getValue().get("weeks"));
            optional_course.setAddress((String) entry.getValue().get("address"));
            optional_coursess.add(optional_course);
        }
        return Result.success(optional_coursess);
    }
}
